<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Examination_model extends MY_Model
{
    public function __construct() {
        parent::__construct();
    }
    public function getExaminationList($per_page,$limit,$fields = " * ")
    {
        if($per_page){
                $per_page = ($per_page-1)*$limit;
        }   
        $limit = " limit {$per_page},{$limit}";
        $sql = "SELECT {$fields} FROM blues_test_question  ORDER BY CreateTime ASC {$limit}";
        $query = $this->db->query($sql);
        
        return $query->result_array();
    }
    public function getExaminationListCnt(){
        $sql = "select paperID  from blues_test_paper WHERE isDel = 0 ";
        $query = $this->db->query($sql);
        $query->num_rows();
        return $query->num_rows();
    }

     //编辑
    public function edit($id,$data = array()){
        if($id){
            $this->db->where("paperID",$id);
            $res = $this->db->update("blues_test_paper",$data);
        }else{
            $res = $this->db->insert("blues_test_paper",$data);
        }
        return $res;
    }

    public function del($id){
        $this->db->where("paperID",$id);
        $data = array(
            'isDel'=>1
        );
        $res = $this->db->update("blues_test_paper",$data);
        return $res;
    }

     //根据id获取广告信息
    public function getExaminationInfo($id){
        $this->db->where("paperID",$id);
        $query = $this->db->get("blues_test_paper");
        return $query->row_array();
    }

    public function editStatus($data = array()){
        $this->db->where("paperID",$data['id']);
        if($data['type'] == "onlinestatus"){
            $data = array(
                'onlinestatus'=>$data['onlinestatus']
            );
        }
        if($data['type'] == "isDel"){
            $data = array(
                'isDel'=>1
            );
        }

        $res = $this->db->update("blues_test_paper",$data);
        return $res;
    }

    public function editQuestion($questionID, $data = [])
    {
        if($questionID)
        {
            //编辑
            $this->db->where("questionID",$questionID);
            $res = $this->db->update($data);
        }else{
            //添加
            $this->db->insert("blues_test_question",$data);
            $res = $this->db->insert_id();
        }
        return $res;
    }
    public function editOption($optionID, $data = [])
    {
        if($optionID)
        {
            //编辑
            $this->db->where("optionID",$optionID);
            $res = $this->db->update($data);
        }else{
            //添加
            $this->db->insert("blues_question_option",$data);
            $res = $this->db->insert_id();
        }
        return $res;
    }

    public function getQuestionList($newsID)
    {
        $sql = "SELECT * FROM blues_test_question WHERE newsID = {$newsID} ORDER BY questionID ASC";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function getOptionList($questionID)
    {
        $sql = "SELECT * FROM blues_question_option WHERE questionID={$questionID}";
        $query = $this->db->query($sql);
        return $query->result_array();

    }

    public function answer($data){
        $this->db->insert("blues_question_answer",$data);
        return  $this->db->insert_id();
    }

    public function hasAnswered($data)
    {
        $sql = "SELECT * FROM blues_test_question ques,blues_question_answer ans  WHERE ques.questionID=ans.questionID AND  ans.AccountID = '".$data["AccountID"]."' AND ques.newsID={$data['newsID']}";
        $query = $this->db->query($sql);
        if($query->num_rows())
        {
            return 1;
        }else{
            return 0;
        }
    }

    public function getQuestionById($questionID,$questionType){
        $sql = "SELECT * FROM blues_test_question WHERE questionID = {$questionID} AND questionType = {$questionType}";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }

    public function getMyAnswer($questionID,$AccountID){
        $sql = "SELECT * FROM blues_question_answer WHERE questionID = {$questionID} AND AccountID = '".$AccountID."'";
        $query = $this->db->query($sql);
        return $query->row_array();
    }
    public function isright($questionID,$questionType,$AccountID)
    {
        $result = 0;
        if($questionType == 1)
        {
            $sql = "SELECT * FROM blues_test_question ques,blues_question_answer ans WHERE ques.questionID=ans.questionID AND ans.AccountID="."'{$AccountID}'"." AND ques.answer=ans.answer AND ques.questionID={$questionID}";
            $query = $this->db->query($sql);
            $result =  $query->num_rows();
            //单选
        }elseif ($questionType == 2) {
             $ans_sql = "SELECT answer FROM blues_question_answer WHERE questionID={$questionID} and AccountID = "."'{$AccountID}'";
             $ans_query = $this->db->query($ans_sql);
             $answer = $ans_query->row_array();
             if($answer)
             {
                $sql = "SELECT * FROM blues_test_question ques,blues_question_answer ans WHERE ques.questionID=ans.questionID AND ans.AccountID="."'{$AccountID}'"." AND ques.answer like '%".$answer['answer']."%' AND ques.questionID={$questionID}";
                $query = $this->db->query($sql);
                $result = $query->num_rows();
             }
             
            //填空
        }else{
            $sql = "SELECT * FROM blues_test_question ques,blues_question_answer ans WHERE ques.questionID=ans.questionID AND ans.AccountID="."'{$AccountID}'"." AND ques.answer=ans.answer AND ques.questionID={$questionID}";
            $query = $this->db->query($sql);
            $result =  $query->num_rows();
            //选图
        }
        return $result;
    }



}


